package com.moji.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.moji.common.entity.MyPage;
import com.moji.common.utils.PageUtil;
import com.moji.entity.MjAboutUs;
import com.moji.entity.MjBanner;
import com.moji.entity.MjBasicInfo;
import com.moji.entity.UploadFiles;
import com.moji.entity.vo.CommonIndexDTO;
import com.moji.mapper.MjAboutUsMapper;
import com.moji.mapper.MjBannerMapper;
import com.moji.service.MjBannerService;
import org.apache.http.util.TextUtils;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

/**
 * Banner(MjBanner)表服务实现类
 *
 * @author makejava
 * @since 2024-07-21 14:35:18
 */
@Service("mjBannerService")
public class MjBannerServiceImpl extends ServiceImpl<MjBannerMapper, MjBanner> implements MjBannerService {
    @Resource
    private MjBannerMapper mjBannerDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public MjBanner queryById(Long id) {
        return this.mjBannerDao.selectById(id);
    }

    /**
     * 分页查询
     *
     * @param mjBanner 筛选条件
     * @return 查询结果
     */
    @Override
    public MyPage<MjBanner> queryByPage(CommonIndexDTO mjBanner) {
        PageHelper.startPage(mjBanner.getPageNum(), mjBanner.getPageSize());
        LambdaQueryWrapper<MjBanner> queryWrapper = new LambdaQueryWrapper<>();
        if (mjBanner.getPosition() != null) {
            queryWrapper.eq(MjBanner::getPosition, mjBanner.getPosition());
        }
        queryWrapper.eq(MjBanner::getStatus,1);
        queryWrapper.orderByAsc(MjBanner::getSort);
        queryWrapper.orderByDesc(MjBanner::getCreateTime);
        List<MjBanner> page = this.list(queryWrapper);
        PageInfo<MjBanner> info = new PageInfo<>(page);
        return PageUtil.PageInfo2MyPage(info);
    }

    /**
     * 新增数据
     *
     * @param mjBanner 实例对象
     * @return 实例对象
     */
    @Override
    public MjBanner insert(MjBanner mjBanner) {
        this.mjBannerDao.insert(mjBanner);
        return mjBanner;
    }

    /**
     * 修改数据
     *
     * @param mjBanner 实例对象
     * @return 实例对象
     */
    @Override
    public MjBanner update(MjBanner mjBanner) {
        LambdaQueryWrapper<MjBanner> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MjBanner::getId,mjBanner.getId());
        this.mjBannerDao.update(mjBanner,queryWrapper);
        return this.queryById(mjBanner.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.mjBannerDao.deleteById(id) > 0;
    }
}
